﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace VerficationsWPF.HWControls
{
    /// <summary>
    /// ButifulDialog.xaml 的交互逻辑
    /// </summary>
    public partial class ButifulDialog : Window
    {
        public ButifulDialog()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        /// <summary>
        /// 以指定控件为中心进行显示。
        /// </summary>
        /// <param name="element"></param>
        public bool? ShowAt(FrameworkElement element)
        {
            if (double.IsNaN(element.ActualWidth) || double.IsNaN(element.ActualHeight))
            {
                WindowStartupLocation = WindowStartupLocation.CenterScreen;
                return ShowDialog();
            }
            else
            {
                WindowStartupLocation = WindowStartupLocation.Manual;
                var leftTop = element.PointToScreen(new Point(0, 0));
                
                this.Left = leftTop.X + (element.ActualWidth - this.Width) / 2;
                this.Top = leftTop.Y + (element.ActualHeight - this.Height) / 2;
                return ShowDialog();
            }
            return null;
        }

        bool isLeftMouseDown = false;
        Point lastMousePosition = new Point();
        Point lastWindowPosition = new Point();

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if(e.LeftButton == MouseButtonState.Pressed)
            {
                var win = Window.GetWindow(this);
                isLeftMouseDown = true;
                lastMousePosition = this.PointToScreen(Mouse.GetPosition(this));
                lastWindowPosition = new Point(win.Left, win.Top);
                Debug.WriteLine($"{PointToScreen(Mouse.GetPosition(this))}  {lastMousePosition}");
            }
        }

        private void Border_MouseMove(object sender, MouseEventArgs e)
        {
            if (isLeftMouseDown)
            {
                Debug.WriteLine($"{Mouse.GetPosition(this)} {this.PointToScreen(Mouse.GetPosition(this))}");
                // 新的鼠标屏幕坐标
                var new_pos = this.PointToScreen(Mouse.GetPosition(this));

                Point offset = new Point(new_pos.X - lastMousePosition.X, new_pos.Y - lastMousePosition.Y);
                var win = Window.GetWindow(this);
                win.Left = lastWindowPosition.X + offset.X;
                win.Top = lastWindowPosition.Y + offset.Y;
            }
        }

        private void Border_MouseUp(object sender, MouseButtonEventArgs e)
        {
            isLeftMouseDown = false;
        }
    }
}
